package com.tamartech.greendb.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;

import org.apache.log4j.Logger;

import com.Ostermiller.util.CSVParser;
import com.Ostermiller.util.LabeledCSVParser;

// this is the one to use for the last minute postal voting only lists that we get from RBBC for the postal voters leaflet
// there are other versions around, but this one is modified to use the new data file format introduced before the 2013 election
public class PostalVoterFilter
{
    private static Logger logger = Logger.getLogger (PostalVoterFilter.class);
    
    private static String HEAD_ADDRESS = "PropertyFullAddress";
    private static String HEAD_FIRST_NAMES = "ElectorForename";
    private static String HEAD_SURNAME = "ElectorSurname";
    private static String HEAD_VOTE_TYPE = "AVDescription";
    private static String HEAD_ELECTION_TYPE = "AVType";
    
    private static String POSTAL_VOTE_TYPE = "Postal";
    
    private static String LOCAL_ELECTIONS_ONLY = "Permanent (Local Only)";
    private static String NON_LOCAL_ELECTIONS_ONLY = "Permanent (Non local)";

	public static void main(String[] args) throws Exception
	{
        File inFile = new File (args[0] + ".csv");
        File outFile = new File (args[0] + "-mailmerge.csv");
	    
        LabeledCSVParser csv = new LabeledCSVParser (new CSVParser (new FileInputStream (inFile)));
//        csv.changeDelimiter('\t'); // I have commas this year, but apparently it should be tabs
        
        PrintWriter out = new PrintWriter (outFile);
        
        String curAddress = ""; // i.e. initialise to empty one
        String names = "";
        
        while(csv.getLine() != null)
        {
            if (csv.getValueByLabel(HEAD_VOTE_TYPE).trim().contains(POSTAL_VOTE_TYPE) &&
                isPostalLocal (csv.getValueByLabel(HEAD_ELECTION_TYPE).trim()))
//                isPostalNonLocal (csv.getValueByLabel(HEAD_ELECTION_TYPE).trim()))
            {
                String address = csv.getValueByLabel(HEAD_ADDRESS).trim();
                
                if (address.equals(curAddress))
                {
                    names += " & " + csv.getValueByLabel(HEAD_FIRST_NAMES).trim().charAt(0) + " " + csv.getValueByLabel(HEAD_SURNAME).trim();
                }
                else
                {
                    if (!names.isEmpty())
                        out.println (createOutLine (names, curAddress));
                    
                    curAddress = address;
                    names = csv.getValueByLabel(HEAD_FIRST_NAMES).trim().charAt(0) + " " + csv.getValueByLabel(HEAD_SURNAME).trim();
                }
            }
        }
        
        if (!names.isEmpty())
            out.println (createOutLine (names, curAddress));

        csv.close();
        out.close();

	}
	
    private static boolean isPostalLocal (String postal)
    {
        return !(NON_LOCAL_ELECTIONS_ONLY.equals(postal));
    }
    
    private static boolean isPostalNonLocal (String postal)
    {
        return !(LOCAL_ELECTIONS_ONLY.equals(postal));
    }
    
	private static String createOutLine (String names, String address)
	{
        StringBuffer outLine = new StringBuffer();
        
        outLine.append(names + "\t");
        outLine.append(address + "\t");
        
        return outLine.toString();
	}
	
//    public static String toMixed(String input)
//    {
//        StringBuffer buff = new StringBuffer(input.length());
//        boolean isWordStarted = false;
//        for (int i = 0; i < input.length(); i++)
//        {
//            char c = input.charAt(i);
//            if (Character.isLetter(c))
//            {
//                if (isWordStarted)
//                    c = Character.toLowerCase(c);
//                else
//
//                    c = Character.toUpperCase(c);
//                isWordStarted = true;
//            }
//            else
//            {
//                isWordStarted = false;
//            }
//            buff.append(c);
//        }
//        return buff.toString();
//    }
} 